package com.sunchangming.uniqname;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class ClientIoHandler extends IoHandlerAdapter {
	private final RpcChannel rpcChannel;

	public ClientIoHandler(RpcChannel rpcChannel) {
		super();
		this.rpcChannel = rpcChannel;
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		rpcChannel.setRes(null);
		super.sessionClosed(session);
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		if (rpcChannel != null)
			rpcChannel.setRes(null);
		super.exceptionCaught(session, cause);
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		rpcChannel.setRes(((Res) message).msg);
	}

}
